$(function () {
    page();
    load();
})

function page() {
    $("#tab_data").bootstrapTable({
        //请求方式  get请求中文会乱码，建议使用post请求
        method: 'post',
        //请求头部的设置
        contentType: 'application/json',
        //url
        url: '/Authorization.do/Page',
        //设置交替行样式
        striped: true,
        //初始显示页号
        pageNumber: 1,
        //设置表格高度
        //height: 350,
        //是否显示分页
        pagination: true,
        //设置是服务端分页还是客户端分页
        sidePagination: 'server',
        //页大小
        pageSize: 5,
        //分页列表
        pageList: [3, 5, 6, 10],
        //上传到服务端的参数
        queryParams: function (params) {
            let auname = $("#auname").val();
            return {
                num: params.offset / params.limit + 1,
                size: params.limit,
                obj: {auname: auname}
            }
        },
        // 定义列名
        columns: [
            {title: "权限编号", field: "auid", align: "center"},
            {title: "权限名称", field: "auname", align: "center"},
            {title: "权限URl", field: "auurl", align: "center"},
            {title: "权限描述", field: "describe", align: "center"},
            {title: "权限状态", field: "status", align: "center",
                formatter: function (vale, row, index) {
                    return vale == 0 ? "正常" : "禁用"
                }
            },
            {
                title: "操作", field: "auid", align: "center",
                formatter: function (vale, row, index) {
                    let auth = JSON.stringify(row);
                    let up_btn = "  <button class=\"btn btn-primary\"  data-toggle=\"modal\" data-target=\"#exampleModal\" onclick='update(" + auth + ")'>修改</button>";
                    let del_btn = "  <button class=\"btn btn-danger\" data-toggle=\"modal\" data-target=\"#delModal\"  onclick=\"LoadRoleDel('" + vale + "')\">删除</button>";

                    return up_btn + "" + del_btn
                }
            }
        ]
    })
    load();
}


function LoadRoleAdd() {
    //重置表单
    $("#AddUpDate")[0].reset();
    $("[name='AuthHidden']").prop("hidden", true);
    $("[name='describe']").text(null);

    $("#exampleModalLabel").text("添加权限信息")
    $("#AddUpDate").prop("action", "/Authorization.do/Add?auths=addauth");
    submitForm();
}
function update(auth) {
    //重置表单
    $("#AddUpDate")[0].reset();
    $("[name='AuthHidden']").prop("hidden", false);

    $.each(auth, function (key, value) {
        $("input[name=" + key + "]").val(value);
    })
    $("[name='describe']").text(auth.describe);
    let s = " <input type=\"radio\" " + (auth.status == 0 ? 'checked' : '') + " name=\"status\" value='0' style=\"width: 16px;height: 16px;\"/>&nbsp;\n" +
        "      <strong>正常</strong> &nbsp;&nbsp;\n" +
        "      <input type=\"radio\" " + (auth.status == 1 ? 'checked' : '') + "  name=\"status\" value='1'  style=\"width: 16px;height: 16px;\"/>&nbsp;\n" +
        "       <strong>禁用</strong>";
    $("#status").html(s);

    $("#exampleModalLabel").text("修改权限信息")
    $("#AddUpDate").prop("action", "/Authorization.do/UpDate?auths=updateauth");
    submitForm();
}
function LoadRoleDel(id) {
    $("#delModal").modal("show");
    $("#delSubmit").one("click", function () {
        $.post("/Authorization.do/Delete?auths=delauth", {id: id}, function (data) {
            $("#delModal").modal("hide");
            $("#tab_data").bootstrapTable("refresh")
        })
    })
}

function submitForm() {
    $("#exampleModal").modal("hide");
    $("#tab_data").bootstrapTable("refresh")
    $("#exampleModal").modal("show");
}
// 刷新页面
function load() {
    $("#tab_data").bootstrapTable("refresh")
}
